package com.angus.modules.content.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.angus.modules.content.domain.LearnRecord;
import com.angus.modules.content.domain.SysUnit;
import com.angus.modules.content.dto.LearnRecordDto;
import com.angus.modules.content.repository.*;
import com.angus.modules.content.service.ILearnRecordService;
import com.angus.modules.content.vo.LearnRecordVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Comparator;
import java.util.List;

/**
 * @author liyanan
 * @date 2024/11/27 3:45 PM
 * @description
 */
@Service
public class LearnRecordServiceImpl implements ILearnRecordService {

    @Autowired
    private LearnRecordRepository learnRecordRepository;

    @Autowired
    private SysUnitRepository sysUnitRepository;



    @Override
    public void addLearnRecord(Long userId, LearnRecordDto dto) {

        learnRecordRepository.addLearnRecord(userId,
                dto.getCourseCode(),
                dto.getUnitNum(),
                dto.getGroupId(),
                dto.getGroupFlag());
    }

    @Override
    public LearnRecordVo getLearnRecord(Long userId, String courseCode, Integer unitNum) {
        List<SysUnit> list = sysUnitRepository.list(Wrappers.lambdaQuery(SysUnit.class)
                .eq(SysUnit::getCourseCode, courseCode)
                .eq(SysUnit::getUnitNum, unitNum));
        SysUnit unit = list.stream().max(Comparator.comparingLong(SysUnit::getGroupId)).orElse(null);
        List<LearnRecord> learnRecords = learnRecordRepository.getLearnRecord(userId, courseCode, unitNum);
        LearnRecordVo recordVo = new LearnRecordVo();
        if (CollUtil.isNotEmpty(learnRecords)) {
            LearnRecord learnRecordMax = learnRecords.stream().max(Comparator.comparingLong(LearnRecord::getGroupId)).orElse(null);
            if (learnRecordMax != null) {
                BigDecimal rate = new BigDecimal(learnRecordMax.getGroupId()).divide(new BigDecimal(unit.getGroupId()), RoundingMode.HALF_UP);
                recordVo.setCourseCode(learnRecordMax.getCourseCode());
                recordVo.setUnitNum(learnRecordMax.getUnitNum());
                recordVo.setCreateTime(learnRecordMax.getCreateTime());
                recordVo.setMaxGroupId(unit.getGroupId());
                recordVo.setLearnMaxGroupId(learnRecordMax.getGroupId());
                recordVo.setRate(rate);
            }
        }else{
            recordVo.setCourseCode(courseCode);
            recordVo.setUnitNum(unitNum);
            recordVo.setMaxGroupId(unit.getGroupId());
            recordVo.setLearnMaxGroupId(1L);
            recordVo.setRate(BigDecimal.ZERO);
        }
        return recordVo;
    }

    @Override
    public Boolean queryLearnRecord(LearnRecordDto dto,Long userId) {
        long count = learnRecordRepository.count(Wrappers.lambdaQuery(LearnRecord.class)
                .eq(LearnRecord::getUserId, userId)
                .eq(LearnRecord::getCourseCode, dto.getCourseCode())
                .eq(LearnRecord::getGroupFlag, dto.getGroupFlag())
        );
        return count > 0;
    }


}
